package com.boc.langxiang.presenter.me;

import android.widget.PopupWindow;

import com.boc.langxiang.contract.me.OrdersWaitPayFragmentContract;
import com.boc.langxiang.entity.request.CancelOrdersRequest;
import com.boc.langxiang.entity.request.OrdersWaitPayRequest;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.entity.response.OrdersWaitPayContent;
import com.boc.langxiang.entity.response.ProductListEntity;
import com.boc.langxiang.net.ApiService;
import com.boc.langxiang.net.HandleErrorSubscriber;
import com.boc.langxiang.net.NetClient;

import java.util.List;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by admin on 2016/6/24.
 */
public class OrdersWaitPayFragmentPresenter implements OrdersWaitPayFragmentContract.Presenter {

    private final ApiService mService;
    private PopupWindow mCancelOrders;
    private OrdersWaitPayFragmentContract.View mView;

    private long currentCancelTime;
    private long lastCancelTime = 0l;


    public OrdersWaitPayFragmentPresenter(OrdersWaitPayFragmentContract.View view) {
        mService = NetClient.getInstance().net().create(ApiService.class);
        this.mView = view;
    }


    @Override
    public void toPayOrders(String page) {
        OrdersWaitPayRequest request = new OrdersWaitPayRequest();
        request.page = page;
        Observable<BaseResponse<OrdersWaitPayContent>> toPayOrders = mService.toPayOrders(request.params());
        toPayOrders.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<OrdersWaitPayContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<OrdersWaitPayContent> response) {
                        OrdersWaitPayContent content = response.getContent();
                        if (content == null) {
                            mView.getPayOrders(null);
                        } else {
                            List<ProductListEntity> list = content.getList();
                            mView.getPayOrders(list);
                        }
                    }
                });

    }

    /**
     * 取消订单
     *
     * @param id
     */
    @Override
    public void cancelOrders(String id) {
        CancelOrdersRequest cancelOrdersRequest = new CancelOrdersRequest();
        cancelOrdersRequest.id = id;
        Observable<BaseResponse> cancelOrders = mService.cancelOrders(cancelOrdersRequest.params());
        cancelOrders.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse>(mView) {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        mView.cancelOrdersSuccess();
                    }
                });
    }
}
